class Confirmation extends egret.DisplayObjectContainer {

	/****************************** 资源与loading配置 ***************************/

    static resUrl: string   = "resource/default.res.json";
    static resRoot: string  = "resource/";
    static resGroup: string = "code";
    static loading: any     = LoadingUI;


	public constructor() {
		super();
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createGameScene, this);
	}
	//输入框添加背景
	private layTxBg(tx: egret.TextField): void {
		var shp: egret.Shape = new egret.Shape();
		shp.graphics.beginFill(0xffffff);
		shp.graphics.drawRect(tx.x, tx.y, tx.width, tx.height);
		shp.graphics.endFill();
		this.addChild(shp);
	}

	/**********************************逻辑*************************************/
	private createGameScene() {
		//背景
		var backgroundImg = creatImg("blue_bg_png");
		this.addChild(backgroundImg);
		//创建背景图片
		var shuiwen = creatImg("shuiwen_png");
		shuiwen.width = 640;
		shuiwen.height = 1136;
		shuiwen.scaleX = 1.01;
		shuiwen.scaleY = 1.1;
		shuiwen.anchorOffsetX = this.stage.$stageWidth * .5;
		shuiwen.anchorOffsetY = this.stage.$stageHeight * .5;
		shuiwen.x = shuiwen.width * .5;
		shuiwen.y = shuiwen.height * .5;
		this.addChild(shuiwen);
		//遮罩
		var rect:egret.Rectangle = new egret.Rectangle(0,0,640,1136);
		this.stage.mask = rect;

		var move = egret.Tween.get(shuiwen, { loop: true });
		move.to({ x: shuiwen.width * .5 + 5, y: shuiwen.height * .5 + 60 }, 7000).wait(500).to({ x: shuiwen.width * .5, y: shuiwen.height * .5 }, 7000);
		//logo
		var logo = creatImg("logo_03_png");
		this.addChild(logo);
		logo.x = 414;
		logo.y = 38;

		//title
		var title = createWord(80, 204, 52, 0xfffffe, "加入安联会员大家庭\n即刻抽奖!", true,"left");
		this.addChild(title);

		//number
		let numberIpt = new egret.Sprite();
		numberIpt.x = 80;
		numberIpt.y = 400;
		this.addChild(numberIpt);
		var wordOne = createWord(0, 0, 32, 0xfffffe, "请输入您的手机号", true);
		numberIpt.addChild(wordOne);
		//输入框背景
		var rectBg = creatRect(80, 460, 452, 70, 0xffffff, 0, 0xffffff);
		this.addChild(rectBg);
		var txInput: egret.TextField = new egret.TextField;
		txInput.type = egret.TextFieldType.INPUT;
		txInput.width = 432;
		txInput.height = 70;
		txInput.size = 32;
		txInput.fontFamily = 'Microsoft YaHei';
		txInput.x = 100;
		txInput.y = 460;
		txInput.textColor = 0x0f0f0f;
		txInput.verticalAlign = "middle";
		this.layTxBg(txInput);
		this.addChild(txInput);

		//验证码
		let sixIpt = new egret.Sprite();
		sixIpt.x = 80;
		sixIpt.y = 664;
		this.addChild(sixIpt);
		var wordTwo = createWord(0, 0, 32, 0xfffffe, "短信验证码", true);
		sixIpt.addChild(wordTwo);

		//输入框背景
		var rectBgTwo = creatRect(80, 725, 452, 70, 0xffffff, 0, 0xffffff);
		this.addChild(rectBgTwo);
		var txInputTwo: egret.TextField = new egret.TextField;
		txInputTwo.type = egret.TextFieldType.INPUT;
		txInputTwo.width = 432;
		txInputTwo.height = 70;
		txInputTwo.size = 32;
		txInputTwo.fontFamily = 'Microsoft YaHei';
		txInputTwo.x = 100;
		txInputTwo.y = 725;
		txInputTwo.textColor = 0x0f0f0f;
		txInputTwo.verticalAlign = "middle";
		this.layTxBg(txInputTwo);
		this.addChild(txInputTwo);

		//按钮
		var buttonOne = creatText("获取验证码", 42, 0xfe3c63, "showbtn_03_png", 275, 80);
		var buttonTwo = creatText("点击验证", 56, 0xfe3c63, "yanzhengbig_03_png", 440, 126);
		this.addChild(buttonOne);
		this.addChild(buttonTwo);
		buttonOne.x = 80;
		buttonOne.y = 555;
		buttonTwo.x = 80;
		buttonTwo.y = 832;
		//倒计时
		// let times = 61;
		// var time = creatText(times, 22, 0xffffff, "", 100, 50);
		// setInterval(() => {
		// 	times-=1;
		// 	this.addChild(time);
		// }, 1000)
		
		
		//输入手机号码获取短信验证
		var authCode;
		buttonOne.touchEnabled = true;
		buttonOne.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
			egret.Tween.get(buttonOne).to({ alpha: .8 }, 100);
			if (!(/^1(3|4|5|7|8)\d{9}$/.test(txInput.text))) {
				//创建信息弹出层
				let dialogs: egret.DisplayObjectContainer = Dialogs.message([
					{
						title: "Warning",
						content: "1.请确保输入正确格式的手机号码。\n2.请确保输入11位数的手机号码。\n3.请确保您的网络畅通。"
					}
				]);
				//打开信息弹出层
				Effect.fadeIn(dialogs, this.stage);
			} else {
				Utils.smsCheck({
					url: "http://swin.bluej.cn/index.php/Home/app/verification_code.html",
					params: { "phone": txInput.text }
				});
			}
		}, this)
		buttonOne.addEventListener(egret.TouchEvent.TOUCH_END, function () {
			egret.Tween.get(buttonOne).to({ alpha: 1 }, 100);
		}, this)

		//输入短信验证码
		buttonTwo.touchEnabled = true;
		buttonTwo.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
			egret.Tween.get(buttonTwo).to({ alpha: .8 }, 100);
			Utils.smsCheck({
				url: "http://swin.bluej.cn/index.php/Home/app/verification_phone.html",
				params: { "phone": txInput.text, "code": txInputTwo.text }
			}, function (res) {
				if (res.state == 1) {
					ScenesMgmt.self.changeScene(award);
				} else {
					//创建信息弹出层
					let dialogs: egret.DisplayObjectContainer = Dialogs.message([
						{
							title: "Warning",
							content: "1.请确保输入正确格式的验证码。\n2.请确保输入5位数的数字。\n3.请五分钟有效时间内输入验证码。"
						}
					]);
					//打开信息弹出层
					Effect.fadeIn(dialogs, this.stage);
				}
			}.bind(this));
		}, this)
		buttonTwo.addEventListener(egret.TouchEvent.TOUCH_END, function () {
			egret.Tween.get(buttonTwo).to({ alpha: 1 }, 100);
		}, this)
	}
}